package com.netease.homework.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.netease.homework.service.IUserService;

/**
 * 
 * @author smart
 * @时间 2019年3月8日
 * @描述 用户接口
 */
@Controller
@RequestMapping("/user")
public class UserCtrl {

	@Autowired
	@Qualifier("UserService")
	private IUserService userService;

	
	@RequestMapping(value = "/getUserById", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getUser(HttpServletRequest request, Integer id) {
		Map<String, Object> returnMap = new HashMap<String, Object>();

		try {
			Map<String, Object> result = userService.getUserById(id);

			returnMap.put("value", result.get("value"));
			returnMap.put("message", result.get("message"));
			returnMap.put("success", true);
		} catch (Exception e) {
			returnMap.put("message", "异常：失败!");
			returnMap.put("success", false);
			e.printStackTrace();
		}

		return returnMap;
	}

	@RequestMapping(value = "/getUserByName", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getUser(HttpServletRequest request, String name) {
		Map<String, Object> returnMap = new HashMap<String, Object>();

		try {
			Map<String, Object> result = userService.getUserByName(name);

			returnMap.put("value", result.get("value"));
			returnMap.put("message", result.get("message"));
			returnMap.put("success", true);
		} catch (Exception e) {
			returnMap.put("message", "异常：失败!");
			returnMap.put("success", false);
			e.printStackTrace();
		}

		return returnMap;
	}

	@RequestMapping(value = "/addUser", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addUser(HttpServletRequest request, String name, String password) {
		Map<String, Object> returnMap = new HashMap<String, Object>();

		try {
			Map<String, Object> result = userService.addUser(name, password);

			returnMap.put("value", result.get("value"));
			returnMap.put("message", result.get("message"));
			returnMap.put("success", true);
		} catch (Exception e) {
			returnMap.put("message", "异常：失败!");
			returnMap.put("success", false);
			e.printStackTrace();
		}

		return returnMap;
	}

}
